package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.ItemService;

/**
 * Servlet implementation class ItemDeal
 */
@WebServlet("/files/ItemDeal")
public class ItemDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String SUCCESS_PAGE = "listWP.jsp";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ItemDeal() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");
		String flag=request.getParameter("flag");
		
		switch(flag){
			case "edit":{
				if( !ItemService.itemEdit(request) ) {
					response.sendRedirect("ErrorDeal?error=IE&iid="+request.getParameter("iid"));
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "export":{
				String excel=ItemService.itemExport(request);
				if( excel!=null ) {
					response.sendRedirect("../"+excel);
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "import":{
				if( !ItemService.itemImport(request) ) {
					response.sendRedirect("ErrorDeal?error=IMI");
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "in":{
				if( !ItemService.itemIn(request) ) {
					response.sendRedirect("ErrorDeal?error=II&iid="+request.getParameter("iid"));
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			case "out":{
				if( !ItemService.itemOut(request) ) {
					response.sendRedirect("ErrorDeal?error=IO&iid="+request.getParameter("iid"));
				}
				else {
					response.sendRedirect(SUCCESS_PAGE);
				}
				break;
			}
			default:break;
		}
	}

}
